<template>
    <div class='sidebar'>
        <el-menu
            class='sidebar-el-menu'
            :collapse='collapse'
            background-color='#324157'
            text-color='#bfcbd9'
            active-text-color='#20a0ff'
            unique-opened
            router
        >

            <template  v-for='item in items'>
                <!--   有子菜单-->
                <template  v-if='item.subs'>
                    <!--先遍历父级菜单-->
                    <el-submenu  :index='item.murl' :key='item.murl'>
                        <template slot='title'>
<!--                            <i :class='item.micon'></i>-->
                            <span >{{ item.mname }}</span>
                        </template>
                        <!--再遍历子菜单-->
                        <template v-for='subItem in item.subs'>
                            <el-menu-item
                                :index='subItem.murl'
                                :key='subItem.murl'
                            >
                                {{ subItem.mname }}
                            </el-menu-item>
                        </template>

                    </el-submenu>
                </template>
                <!--没有子菜单 直接遍历顶级节点-->
                <template v-else>
                    <el-menu-item  :key='item.murl'>
<!--                        <i :class='item.micon'></i>-->
                        <span slot='title'>{{ item.mname }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
import bus from '../common/bus';

export default {
    data() {
        return {
            collapse: false,
            items: [

            ]
        };
    },
    computed: {
        onRoutes() {
            return this.$route.path.replace('/', '');
        }
    },
    created() {
        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        bus.$on('collapse', msg => {
            this.collapse = msg;
            bus.$emit('collapse-content', msg);
        });
        //请求菜单渲染
        this.$http.get('/authserver/menuController/getMenuByUser.json').then((res) => {
            if (res.data.status) {
                this.$message.success(res.data.msg);
                this.items = res.data.data;
                console.log(this.items);
            }
        });
    }
};
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}

.sidebar::-webkit-scrollbar {
    width: 0;
}

.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
}

.sidebar > ul {
    height: 100%;
}
</style>
